﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (isName())
        {
            labIsName.Text = "UserName exists!";
            labIsName.ForeColor = System.Drawing.Color.Red;
            RegisterStartupScript("", "<script>alert('Please enter the correct information!')</script>");
        }
        else
        {
            string userName = UserName.Text;

            string userPass = pass.Text;
            //string userPass = FormsAuthentication.HashPasswordForStoringInConfigFile(pass.Text, "MD5");

            string firstName = FirstName.Text;
            string lastName = LastName.Text;
            string email = Email.Text;
            string phone = Phone.Text;
            string address = Address.Text;
            string sec_email = SecEmail.Text;

            string sqlIns = "insert into TableUserInfo values('" + userName + "','" + userPass + "','" + firstName + "','" + lastName + "','" + email + "','" + phone + "','" + address + "','" + sec_email + "')";

            SqlConnection con = new SqlConnection("server=localhost;database=reg_sys;integrated security=true;");

            con.Open();
 
            SqlCommand com = new SqlCommand(sqlIns, con);

            if (com.ExecuteNonQuery() > 0)
            {
                RegisterStartupScript("", "<script>alert('Register Success!')</script>");

                UserName.Text = pass.Text = Repass.Text = FirstName.Text = LastName.Text = Email.Text = Phone.Text = Address.Text = SecEmail.Text = "";
                labIsName.Text = "";
                Server.Transfer("Default.aspx");
            }
            else
            {
                RegisterStartupScript("", "<script>alert('Please enter the correct information!')</script>");
            }
        }

        //MembershipCreateStatus Mystatus;
        //MembershipUser newuser = Membership.CreateUser(UserName.Text, pass.Text, Email.Text, PassQe.Text, PassAn.Text, true, out  Mystatus);
        //if (newuser == null)
        //{
        //    Response.Write(Mystatus.ToString());
        //    return;
        //}
        //Guid Newguid = new Guid(newuser.ProviderUserKey.ToString());
        //SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
        //SqlTransaction transql = null;
        //SqlCommand cmm = new SqlCommand();
        //try
        //{
        //    conn.Open();
        //    transql = conn.BeginTransaction();
        //    cmm.Connection = conn;
        //    cmm.Transaction = transql;
        //    string str_sql = "";
        //    str_sql += " INSERT INTO UserY (serID,Address,Phone) ";
        //    str_sql += " VALUES (@Newguid,'" + Address.Text + "','" + Phone.Text + "')";
        //    cmm.CommandText = str_sql;
        //    cmm.Parameters.Add(new SqlParameter("@Newguid", SqlDbType.UniqueIdentifier));
        //    cmm.Parameters["@Newguid"].Value = Newguid;
        //    cmm.ExecuteNonQuery();
        //    transql.Commit();
        //}
        //catch (Exception ex)
        //{
        //    transql.Rollback();
        //    Membership.DeleteUser(UserName.Text);
        //    Response.Write(ex.ToString());
        //}
        //finally
        //{
        //    FormsAuthentication.SetAuthCookie(UserName.Text, true);
        //    FormsAuthentication.RedirectFromLoginPage(UserName.Text, true);
        //}
    }

    protected bool isName()
    {
        bool blIsName = false;

        string sqlSel = "select count(*) from TableUserInfo where UserName='" + UserName.Text + "' ";
 
        SqlConnection con = new SqlConnection("server=localhost;database=reg_sys;integrated security=true;");
   
        con.Open();
 
        SqlCommand com = new SqlCommand(sqlSel, con);
 
        if (Convert.ToInt32(com.ExecuteScalar()) > 0)
        {
            blIsName = true;
        }
        else
        {
            blIsName = false;
        }

        return blIsName;
    }


    protected void UserName_TextChanged(object sender, EventArgs e)
    {

        if (isName())
        {
            labIsName.Text = "UserName exists!";
            labIsName.ForeColor = System.Drawing.Color.Red;
        }
        else
        {
            labIsName.Text = "Could register!";
            labIsName.ForeColor = System.Drawing.Color.Blue;
        }
    }
}
